package com.cashbook.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.aliyun.oss.common.comm.ResponseHandler;
import com.cashbook.pojo.TbInEx;
import com.cashbook.service.InExService;
import com.cashbook.utils.DateUtils;
import com.cashbook.utils.HttpRequestor;

import entity.PageResult;
import entity.Result;
import net.sf.json.JSONObject;

/**
 * controller
 * 
 * @author Administrator
 *
 */
@RestController
@RequestMapping("/openid")
public class OpenIdController {

	@Autowired
	private InExService inexService;
	

	/**
	 * 返回全部列表
	 * 
	 * @return
	 * @throws Exception 
	 */
	@RequestMapping("/findOpenId")
	public String findOpenId(HttpServletRequest request,HttpServletResponse response) throws Exception {
		 String code=request.getParameter("code");
//		String code="033KwR1c0vFRlu1x5n3c0GuN1c0KwR1c";
			System.out.println("---");
	        String appid = "wxca14a4e5cbdfe8f1";
	        String secret = "ceb13ebede7e4c527bcd049f26ab1010";
	        //https://api.weixin.qq.com/sns/jscode2session?appid=APPID&secret=SECRET&js_code=JSCODE&grant_type=authorization_code
	       // String requestUrl = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+secret+"&code="+code+"&grant_type=authorization_code";
	         String requestUrl="https://api.weixin.qq.com/sns/jscode2session?appid="+appid+"&secret="+secret+"&js_code="+code+"&grant_type=authorization_code";
	        //第一次请求 获取access_token 和 openid
	        String  oppid = new HttpRequestor().doGet(requestUrl);
	        System.out.println("oppid is :"+oppid);
	        JSONObject oppidObj =JSONObject.fromObject(oppid);
	        String access_token = (String) oppidObj.get("access_token");
	        String openid = (String) oppidObj.get("openid");
	        String requestUrl2 = "https://api.weixin.qq.com/sns/userinfo?access_token="+access_token+"&openid="+openid+"&lang=zh_CN";
	        String userInfoStr = new HttpRequestor().doGet(requestUrl2);
	        JSONObject wxUserInfo =JSONObject.fromObject(userInfoStr); 
		 
		System.out.println(openid);
		return openid;
	}

	
}
